<template>
  <div class="list">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      class="list-container"
    >
  <!-- <van-cell v-for="item in list" :key="item.id" :title="item.title" /> -->
  <!-- <van-card
    v-for="item in list"
    :key="item.id"
    :price="item.price"
    :num="item.saleNum"
    :desc="item.title"
    :title="item.title"
    :thumb="item.image"
  /> -->
  <div
  v-for="item in list"
    :key="item.id"
    class="list-item"
    is="router-link"
    :to="{name:'detail' ,params:{id:item.id}}"
    tag="div"
  >
    <img :src="item.image" alt="">
    <span>{{item.title}}</span>
    <div>
      <span>{{item.price}}</span>
      <span>{{item.saleNum}}人已购买</span>
    </div>
  </div>
</van-list>
  </div>
</template>

<script>
// vant :List 组件通过loading和finished两个变量控制加载状态，当组件滚动到底部时，会触发load事件并将loading设置成true。此时可以发起异步操作并更新数据，数据更新完毕后，将loading设置成false即可。若数据已全部加载完毕，则直接将finished设置成true即可。
import { getList } from '@/api/list'
export default {
  name: 'List',
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1
    }
  },
  methods: {
    onLoad () {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      getList(this.$route.query.url, this.page).then(res => {
        console.log(res)
        this.list = this.list.concat(res)
        this.page += 1
        this.loading = false
        if (res.length === 0) { // 当加载页面数据返回列表长度为0，说明没有更多数据可以加载了
          this.finished = true
        }
      })
    }
  }
  // created () {
  //   getList(this.$route.query.url).then(res => {
  //     console.log(res)
  //     this.list = res
  //   })
  // }
}
</script>

<style lang="scss" scoped>
  .list{
    &-container {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
    }
    &-item {
      width: 48%;
      margin: 6px 0;
      img{
        max-width: 100%;
      }
    }
  }
</style>
